const router = require('koa-router')();
const query = require("../db/mysql");
const {obj,encrypt, decrypt} = require('../public/util')
const {key,iv} = obj;
//用户收藏了的画作
router.get('/collection',async function(ctx,next){
    try{
        var uId = ctx.query.uId
        console.log(uId);
        uId = decrypt(uId,key,iv);
        console.log(uId);
        let sql = `SELECT pId FROM collection WHERE uId = ?`
        let data = await query(sql,[uId]);
        return ctx.body = {
            status:200,
            data:data
        }
    } catch (err){
        console.log(err)
        return ctx.body = {
            status:500,
            msg:err
        }
    }
})
//得到用户答题记录
router.get('/record',async function(ctx,next){
    try{
        var uId = ctx.query.uId
        const pId = ctx.query.pId
        uId = decrypt(uId,key,iv);
        let sql = `SELECT * FROM record WHERE uId = ?`
        let data = await query(sql,[uId])
        console.log(data);
        return ctx.body = {
            status:200,
            data:data
        }
    } catch (err){
        console.log(err)
        return ctx.body = {
            status:500,
            msg:err
        }
    }
})

router.get('/mywork',async function(ctx,next){
    try{
        const uId = ctx.query.uId
        let sql = `SELECT * FROM mywork WHERE uId = ${uId}`
        let data = await query(sql)
        return ctx.body = {
            status:200,
            data:data
        }
    } catch (err){
        console.log(err)
        return ctx.body = {
            status:500,
            msg:err
        }
    }
})

router.get('/score',async function(ctx,next){
    try{
        let sql = `SELECT uName,score FROM user`
        let data = await query(sql)
        return ctx.body = {
            status:200,
            data:data
        }
    } catch (err){
        console.log(err)
        return ctx.body = {
            status:500,
            msg:err
        }
    }
})

module.exports = router